;( function () {
    "use strict";
   	var param_map = Result.getLocationParamObject();
    var category_id = param_map.category_id;
	 $( function () {
		 loadLeftCategory();
	 });
	 
	 
	 function loadLeftCategory(){
		 var dataList = CATEGORY_LIST;
		 var $template = $("#category_template"),
		 	 $list = $("#category_left_list");
		 $list.empty();
		 $list.append($("#first_template"));
		 if( $.isArray( dataList ) ) {
     		for( var i = 0, l = dataList.length; i < l; i++ ) {
     			var obj = $template.clone().removeAttr( "id" );
                	$list.append( Result.fillData( obj, dataList[i], "field" ));
            }
         }
		 
		 if(category_id == null){
			 $("#category_left_list li.second").eq(0).removeClass("active").addClass("active");
			 var fir = $("#category_left_list li.second").eq(0).find('a').attr('category_id');
			 loadCategoryList(fir);
		 } else {
			 var ind = parseInt(category_id) - 1;
			 $("#category_left_list li.second").eq(ind).removeClass("active").addClass("active");
			 loadCategoryList(category_id);
		 }
		
		 $("#category_left_list li.second").click(function(){
			 $("#category_left_list li.second").removeClass("active");
			 $(this).addClass("active");
			 loadCategoryList($(this).find('a').attr("category_id"));
		 });
	 }
	 
	 
	 
	 function loadCategoryList(category){
		 var dataList = categoryListMap[category];
		 var $template = $("#list_template"),
		 	 $list = $("#listlist");
		 $list.empty();
		 if( $.isArray( dataList ) ) {
     		for( var i = 0, l = dataList.length; i < l; i++ ) {
     			var obj = $template.clone().removeAttr( "id" );
     			$list.append( Result.fillData( obj, dataList[i], "field" ));
            }
         }
		 
		 
		 $("#listlist").find(".to_detail").click(function(){
			 var newid = $(this).attr('race_id');
			 var category_id = $("#category_left_list li.second.active").find('a').attr("category_id");
			 Result.forward("raceDetail.html", {"race_id" : newid, "category_id" : category_id});
		 });
	 }
	
	 
	 
	 
	 window.replace_id = function ( $field, id, rowVal ) {
		$field.attr("category_id", rowVal['id']);
	 };
	 
	 window.replace_image = function ( $field, image, rowVal ) {
		$field.attr("src", image);
	 };
	 
	 window.replace_fee = function ( $field, fee, rowVal ) {
		 if(fee == 0){
			 $field.parent().html("免报名费");
		 }
		 $field.html(fee);
	 };
	 
	 window.replace_title = function ( $field, title, rowVal ) {
		 $field.html(title);
		 $field.attr("race_id",rowVal['id']);
	 };
	 
	 
	 
	 window.replace_status = function ( $field, status, rowVal ) {
		 if(status == 1 ){
			 $field.parent().find(".icon-match-status").addClass("icon-status-active");
			 $field.html("正在报名");
		 } else if (status == 2){
			 $field.addClass("font-color-2");
			 $field.parent().find(".icon-match-status").addClass("icon-status-none");
			 $field.html("报名结束");
		 } else {
			 $field.addClass("font-color-3");
			 $field.parent().find(".icon-match-status").addClass("icon-status-default");
			 $field.html("已比赛");
		 }
	 };
	 
	 
		 
	
}( window ) );
